/*
 * @Author: your name
 * @Date: 2020-10-19 17:30:11
 * @LastEditTime: 2020-11-02 15:17:04
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /Project/node/services/FoodService.js
 */
const boom = require('boom');
const {querySql, queryOne, guard} = require('../utils')
const {validationResult } = require('express-validator');



/**录入商品 */
function commitFood(req,res,next) {

  guard(req,next, () => {
    let { BusinessName, FoodName,FoodImage, price } = req.body; 
      //创建商品表
    const goodSql = 'create table if not exists Good (goodId INT(6) NOT NULL AUTO_INCREMENT,BusinessName VARCHAR(255) NOT NULL, FoodName VARCHAR(255) NOT NULL, FoodImage VARCHAR(255) NOT NULL,  price FLOAT NOT NULL,createTime DATETIME(3) NULL DEFAULT CURRENT_TIMESTAMP(3) ,  PRIMARY KEY (goodId) )'
    querySql(goodSql).then((response) => {
      if (response) {
        let insertSql = `INSERT INTO myDB.Good (BusinessName, FoodName,FoodImage, price) VALUES ("${BusinessName}", "${FoodName}","${FoodImage}", ${price})`
        querySql(insertSql).then((result) =>{
          if (result) {
            res.json({code:200,data:result,msg:'录入成功'})            
          }
        })
      }
    })
  })
}


/**获取商品列表 */
function foodList(req, res, next) {
  let sql = 'select * from myDB.Good';
  querySql(sql).then((result) => {
    res.json({code:200,data:result,msg:'获取成功'})
  })
}


/**获取商品详情 */
function goodDetail(req, res, next) {
  let goodId = req.body.goodId;
  let sql = `select * from myDB.Good where goodId=${goodId}`
  querySql(sql).then((result) => {
    res.json({code:200,data:result[0],msg:'获取成功'})
  })
}

module.exports = {
  commitFood,
  foodList,
  goodDetail
}